namespace Brjnk.GpxExplorer.Computing
{
    using System;
    using Brjnk.GpxExplorer.ViewModel;

    /// <summary>
    /// </summary>
    public class DurationDeltaCalculator
    {
        public static void Calculate(ITrackPoint lastWithTime, bool isLastInSegment, ITrackPoint point)
        {
            if (lastWithTime != null && point.DurationJoined.HasValue)
            {
                TimeSpan delta = (point.DurationJoined.Value - lastWithTime.DurationJoined.Value);
                point.DurationDeltaJoined = delta;

                if (isLastInSegment) point.HasDurationDelta = true;
                else point.HasDurationDelta = false;
            }
            else if (point.DurationJoined.HasValue)
            {
                point.HasDurationDelta = false;
                point.DurationDeltaJoined = TrackPointAttributeValue<TimeSpan>.CreateUnset();
            }
            else
            {
                point.HasDurationDelta = true;
                point.DurationDeltaJoined = TrackPointAttributeValue<TimeSpan>.CreateUnavailable();
            }
        }
    }
}